package com.jl.js.core;

import java.util.HashMap;
import java.util.Map;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.annotations.JSConstructor;
import org.mozilla.javascript.annotations.JSFunction;
import org.mozilla.javascript.annotations.JSGetter;

public class JSHashMap extends ScriptableObject {

	private HashMap<Object, Object> inner = new HashMap<Object, Object>();

	@JSConstructor
	public JSHashMap() {
	}

	@Override
	@JSGetter
	public int size() {
		return inner.size();
	}

	@Override
	@JSFunction
	public boolean isEmpty() {
		return inner.isEmpty();
	}

	@JSFunction
	public void clear() {
		inner.clear();
	}

	@Override
	@JSFunction
	public Object get(Object key) {
		return inner.get(key);
	}

	@JSFunction
	public Object put(Object arg0, Object arg1) {
		return inner.put(arg0, arg1);
	}

	@JSFunction
	public void putAll(JSHashMap arg0) {
		inner.putAll(arg0.getInnerHashMap());
	}

	@JSFunction
	public boolean containsKey(Object arg0) {
		return inner.containsKey(arg0);
	}

	@JSFunction
	public boolean containsValue(Object arg0) {
		return inner.containsValue(arg0);
	}

	@JSFunction
	public Object remove(Object arg0) {
		return inner.remove(arg0);
	}

	@JSFunction
	public Scriptable keySet() {
		Context cx = Context.getCurrentContext();
		Scriptable parent = ScriptableObject.getTopLevelScope(this);
		Object[] objs = inner.keySet().toArray();
		return cx.newObject(parent, "HashSet", new Object[] { objs });
	}

	@JSFunction
	public Scriptable values() {
		Context cx = Context.getCurrentContext();
		Scriptable parent = ScriptableObject.getTopLevelScope(this);
		Object[] objs = inner.values().toArray();
		return cx.newObject(parent, "ArrayList", new Object[] { objs });
	}

	@Override
	public String getClassName() {
		return "HashMap";
	}

	private Map<? extends Object, ? extends Object> getInnerHashMap() {
		return inner;
	}

}
